//
//  FileSize.swift
//  APP
//
//  Created by Rdxer on 2017/12/23.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit

/// fileSizeMode
public enum FileSizeMode {
    case _1024
    case _1000
}
extension FileSizeMode{
    var ary: Double {
        switch self {
        case ._1000:
            return 1000
        case ._1024:
            return 1024
        }
    }
}

/// iOS中 1T == 1000G == 1000 * 1000Mb == 1000 * 1000 * 1000kb == 1000 * 1000 * 1000 * 1000B

/// 一般的计算是 1024

/// 单位 byte  不是 bit
extension Int64{
    public func fileSizeShowString(_ mode:FileSizeMode = ._1000)->String {
        
        let v = Double.init(self)
        
        if v == 0 {
            return "0KB"
        }
        
        if v < mode.ary{
            return NSString.init(format: "%.2fB", v) as String
        }
        
        var lastAry = mode.ary
        
        
        var powV = pow(mode.ary, 2)
        if  v < powV {
            return NSString.init(format: "%.2fKB", v/lastAry) as String
        }
        lastAry = powV
        
        powV = pow(mode.ary, 3)
        if  v < powV {
            return NSString.init(format: "%.2fMB", v/lastAry) as String
        }
        lastAry = powV
        
        powV = pow(mode.ary, 4)
        if  v < powV {
            return NSString.init(format: "%.2fGB", v/lastAry) as String
        }
        lastAry = powV
        
//        powV = pow(mode.ary, 5)
//        if  v < powV {
            return NSString.init(format: "%.2fTB", v/lastAry) as String
//        }
//        lastAry = powV
//
//        return ""
    }
}

